package com.android.theList;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class main extends Activity {
	
	public static final int TAKE_OUT=1;
	public static final int SIT_DOWN=2;
	public static final int DELIVERY=3;
	
	List<Restaurant> model = new ArrayList<Restaurant>();
	ArrayAdapter<Restaurant> adapter = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button save =(Button)findViewById(R.id.btnSave);
        save.setOnClickListener(onSave);
        
        RadioButton rbTakeOut = new RadioButton(this.getApplicationContext());
        rbTakeOut.setText("Take out");
        rbTakeOut.setId(TAKE_OUT);
        rbTakeOut.setChecked(true);
        RadioButton rbSitDown = new RadioButton(this.getApplicationContext());
        rbSitDown.setText("Sit down");
        rbSitDown.setId(SIT_DOWN);
        RadioButton rbDelivery= new RadioButton(this.getApplicationContext());
        rbDelivery.setText("Delivery");
        rbDelivery.setId(DELIVERY);
        
        RadioGroup rg = (RadioGroup)findViewById(R.id.rgTypes);
        rg.addView(rbTakeOut);
        rg.addView(rbSitDown);
        rg.addView(rbDelivery);
        
        ListView list = (ListView)findViewById(R.id.restaurants);
        
        adapter = new ArrayAdapter<Restaurant>(this.getApplicationContext(),
        		android.R.layout.simple_list_item_1, model);
        
        list.setAdapter(adapter);
        
        
    }
    
    private OnClickListener onSave = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			String name = ((EditText)findViewById(R.id.txtName)).getText().toString();
			String address = ((EditText)findViewById(R.id.txtAddress)).getText().toString();
			Restaurant r = new Restaurant();
			r.setName(name);
			r.setAddress(address);
			
			RadioGroup types = (RadioGroup)findViewById(R.id.rgTypes);
			
			switch (types.getCheckedRadioButtonId()) {
			case SIT_DOWN:
				r.setType("sit_down");
				break;
			case TAKE_OUT:
				r.setType("take_out");
				break;
			case DELIVERY:
				r.setType("delivery"); 
				break;
			default:
				break;
			}
			model.add(r);
		}
	};
}